package crawl.http;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import utils.Logger;
import utils.Util;

import index.Indexer;
import index.Term;

public class HtmlIndexer extends Indexer
{
    private Map<String, Integer> urlMap;

    public HtmlIndexer()
    {
        urlMap = Collections.synchronizedMap(new HashMap<String, Integer>());
        readMapFile();
    }

    private void readMapFile()
    {
        File f = Util.getMapFile();
        if (!f.canRead())
        {
            Logger.logException(f.toString() + " cannot be read", true);
        }
        try
        {
            Scanner scan = new Scanner(f);
            while (scan.hasNext())
            {
                Integer value = scan.nextInt();
                scan.next();
                String key = scan.next();
                urlMap.put(key, value);
            }
        }
        catch (FileNotFoundException e)
        {
            Logger.logException(e);
        }
    }

    public void build() throws IOException
    {
        File list = Util.getWordListFile();
        File database = Util.getInvertDocumentFile();
        BufferedWriter listOut = new BufferedWriter(new FileWriter(list));
        BufferedWriter dbOut = new BufferedWriter(new FileWriter(database));
        Collection<Term> terms = Term.getAllTerms();
        for (Term term : terms)
        {
            listOut.write(term.getName() + Util.getLineSeparator());
            listOut.flush();
            dbOut.write(term.toDetailString() + Util.getLineSeparator());
            dbOut.flush();
        }
        listOut.close();
        dbOut.close();
    }
}
